#include <stdio.h>
#include <string>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>

using std::string;
 
 void writeFiles(char * d, char * f)
 {
    DIR             *dip;
    DIR             *dip2;
    struct dirent   *dit;
    struct stat eStat;
    
    if ((dip = opendir(d)) == NULL)   
    {     
        perror("opendir");        
        return ;
    }
  
    while((dit = readdir(dip)) != NULL) 
    {        
        char* tmp= const_cast<char*> ((string(d) + string(dit->d_name) + "/").c_str());        
        if(((dip2 = opendir(tmp)) == NULL))
        {           
           if (strcmp (dit->d_name, f) == 0)
           {
                lstat(dit->d_name, &eStat);                               
                std::cout << "file's in "<< tmp << "\n"<<"size: "<< eStat.st_size <<"\n"<<"user ID of owner:    "<<eStat.st_uid << "\n" <<"group ID of owner: "<<eStat.st_gid << "\n" <<"time of last change: "<<eStat.st_ctime<<"\n"<< std::endl;                
           }
        }            
        else if(!((dip2 = opendir(tmp)) == NULL))
        {
           //printf("found directory: %s\n", dit->d_name);
           if ((strcmp (dit->d_name,".") != 0) && (strcmp (dit->d_name,"..") != 0))           
                writeFiles(tmp, f);           
        }         
    }  
    
    if (closedir(dip) == -1)   
            perror("closedir");    
 }
 
int main(int argc, char *argv[])
{       
    if (argc < 2)
    {
        printf("Usage: %s <directory>\n", argv[0]);
        return 0;
    } 
    
    writeFiles(argv[1], argv[2]);          

    printf("Directory stream is now closed\n");
    return 0;
}
 
